/**
 * @file
 * @auth jinguangguo
 * @date 2017/5/10
 */

import Vue from 'vue';

import commonService from '../../../../common/commonService';
import service from '../service';
import Pager from '../../../../../grsc-base/component/Pager.vue';
import Loading from '../../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../../grsc-base/component/NoData.vue';
import filter from '../../../../../grsc-base/util/filter';
import {PAGE_SIZE} from '../../../../../grsc-base/common/constants';
import Popover from '../../../../../grsc-base/component/Popover.vue';
import lib from '../../../../common/lib';
import AssetDrop from '../AssetDrop.vue'

export default Vue.extend({
    components: {
        pager: Pager,
        loading: Loading,
        noData: NoData,
        Popover: Popover,
        AssetDrop
    },
    computed: {
        addressTip(){
            return this.coinType + " " + this.$t('common.address')
        }
    },
    data() {
        return {
            coinType:'BTC',
            coinList:null,
            list: null,
            isLoading: false,
            filter:filter,

            address: '',
            coinBalance:'--',
            coinLock:'--',

            condition: {
                pageNo: 1,
                pageSize: PAGE_SIZE,
                pageNum: 0,
            },

            hasAuth: true   // 是否已经实名
        }
    },
    created() {
        if (lib.hasAuth() === true) {
            this.hasAuth = true;
            this.coinType = this.$route.params.coinType.toUpperCase();
            this.fetchList();
            this.fetchAddress();
        } else {
            this.hasAuth = false;
        }
    },

    methods: {

        /**
         * 获取充值地址
         * */
        fetchAddress() {
            commonService.getCoinAddress({
                assetCode: this.coinType
            }).done(rep => {
                if(rep.data[0]){
                    this.address = rep.data[0].address;
                }else{
                    this.address = '';
                }
                this.buildQrcode();
            });
        },

        /**
         * 获取数据列表
         */
        fetchList() {
            this.isLoading = true;
            let param = {
                assetCode: this.coinType,
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize
            };
            service.depositCoinDetails(param).done((rep) => {
                let list = rep.data.list;
                if (rep.code == window.CODE.SUCCESS) {
                    this.formatStatusType(list);
                    this.list = list;
                    this.condition.pageNum = rep.data.pageNum;
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {
                this.list = [];
            }).always(()=>{
                this.isLoading = false;
            });
        },
        /**
         * 生成二维码
         */
        buildQrcode(){
            $(this.$refs.qrcode).qrcode({
                render: "canvas", //table方式
                width: 186, //宽度
                height: 186, //高度
                text: this.address //任意内容
            });
        },
        copyAddress($event){
            let target = $($event);
            lib.copy(target);
        },
        /**
         * 分页
         * @param pageNo
         */
        changePage(pageNo) {
            this.condition.pageNo = pageNo;
            this.fetchList();
        },
        formatStatusType(list) {
            let str;
            $.map(list, (item, index) => {
                let statusText='';
                str = item.status;
                if (!str) {

                } else {
                    switch (str) {
                        case 'SUCCESS':
                            statusText = this.$t('asset.common.completed');
                            break;
                        case 'PROCESSING':
                            statusText = this.$t('asset.common.pending');
                            break;
                        case 'FAILURE':
                            statusText = this.$t('asset.common.failed');
                            break;
                        case 'WAITING':
                            statusText = this.$t('asset.common.pending');
                            break;
                        case 'REFUSE':
                            statusText = this.$t('asset.common.refunded');
                            break;
                        default:
                            statusText = str;
                    }
                }
                item.statusText = statusText;
            });
        },


        changeCoinType(coinType){
            this.coinType = coinType;
            this.$router.push({ name: 'deposit', params: { coinType: this.coinType }});
            this.reload();
            // this.fetchList();
            // this.fetchAddress();
            // this.getTotalAssets();
        }
    }
});